package com.ruoyi.assessment.service.impl;

import com.ruoyi.assessment.entity.AssessmentVo;
import com.ruoyi.assessment.service.IAssessmentService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AssessmentServiceImpl implements IAssessmentService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IXlkjIniService iniService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 部门调用接口
     * @param deptIds     自己部门id集合
     * @return
     */
    public List<AssessmentVo> getList(String deptIds){
        List<AssessmentVo> assessmentVoList = new ArrayList<>();

        //查询所有网格员
        List<SysUser> sysUsers = sysUserMapper.selectUserByDeptIds(deptIds);
        for(SysUser sysUser:sysUsers){
            AssessmentVo assessmentVo = new AssessmentVo();

            /**
             * 计算得分
             *      基础分值：50
             *    该部门人数 / 该部门总上报事件数
             *      加分项分值：50
             *    完成总数 / 事件总数  < 20%     分值不加
             *    20% <= 完成总数  / 事件总数  < 50%    分值 + 20
             *    50% <= 完成总数 / 事件总数   < 80%     分值 + 30
             *    80% <= 完成总数 / 事件总数            分值 + 40
             */
            Integer source = 0;

            Long deptId = sysUser.getDeptId();
            SysDept sysDept = sysDeptService.selectDeptById(deptId);
            List<String> list = sysDeptMapper.selectIdByParentId(sysDept.getParentId());
            //设置该部门总人数
            assessmentVo.setUserNum(list.size());
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            XlkjIni ini = new XlkjIni();
            ini.setDepartmentList(str);
            List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
            //设置该部门总上报事件数
            assessmentVo.setIncidentNum(xlkjInis.size());

            ini.setUserId(sysUser.getUserId());
            List<XlkjIni> xlkjInis1 = iniService.selectXlkjIniList(ini);
            //设置我的上报数
            assessmentVo.setIniNum(xlkjInis1.size());

            ini.setStatus("2");
            List<XlkjIni> xlkjInis2 = iniService.selectXlkjIniList(ini);
            //设置我的完成数
            assessmentVo.setIncidentSucc(xlkjInis2.size());

            //计算基础分
            int pj = assessmentVo.getIncidentNum() / assessmentVo.getUserNum();
            if(assessmentVo.getIniNum() >= pj){
                source += 50;
            }else {
                source += assessmentVo.getIniNum() / pj * 50;
            }

            //计算加分项
            int fl;
            Integer incidentNum = assessmentVo.getIncidentNum();
            if(incidentNum == 0){
                fl = 0;
            }else {
                fl = assessmentVo.getIncidentSucc()/assessmentVo.getIncidentNum();
            }

            if(fl < 0.2){

            }else if(fl >= 0.2 && fl < 0.5){
                source += 20;
            }else if(fl < 0.8 && fl >= 0.5){
                source += 30;
            }else {
                source += 50;
            }
            //设置总得分
            assessmentVo.setSourceNum(source);

            assessmentVoList.add(assessmentVo);
        }
        return assessmentVoList;
    }

}
